import React, { Component } from 'react';
import StorageUtils from '../Utils/StorageUtils'

export default class ToastUtils extends React.Component
{

    static checkDateSelect(content, reason)
    {
        if (content == '请选择')
        {
            throw reason;
        }
    }

    static validTime(startTime, endTime)
    {
        // var date1 = new Date(startTime.replace(/-/g, '/'));
        // var date2 = new Date(endTime.replace(/-/g, '/'));
        var date1 = Date.parse(startTime);
        var date2 = Date.parse(endTime);
        if (date1 > date2)
        {
            throw '结束时间不能小于开始时间';
        }

    }

    static checkDeviceCodeValid(code)
    {
        if (code.length < 8)
        {
            throw '设备码长度不正确';
        }
    }

    static checkShareSelf(rcvphone, phone)
    {
        if (rcvphone == phone)
        {
            throw '不能分享给自己';
        }
    }

    static checkTransferSelf(rcvphone, phone)
    {
        if (rcvphone == phone)
        {
            throw '不能移交给自己';
        }
    }

    static checkPhoneRegex(phonNum)
    {
        var MATCH_PHONE_REGEX = "((13)|(14)|(15)|(16)|(17)|(18)|(19))\\d{9}$";
        if (!new RegExp(MATCH_PHONE_REGEX).test(phonNum))
        {
            throw '请输入正确的手机号';
        }
    }

    static checkSMSCodeRegex(code)
    {
        var SMS_SIX_CODER = "\\d{6}";
        if (!new RegExp(SMS_SIX_CODER).test(code))
        {
            throw '短信验证码长度为6位数字';
        }
    }

    static checkAllNumber(content)
    {
        var MATCH_NUMBER_REGEX = /^[0-9]+$/;
        return new RegExp(MATCH_NUMBER_REGEX).test(content);
    }

    static checkGatewayNameValid(text)
    {
        var pattern = /^([a-zA-Z0-9_])+$/gi;
        var flag = pattern.test(text);
        if (!flag)
        {
            throw '设备编号只能输入字母和数字';
        }
    }

    static checkInputValid(text)
    {
        var pattern = /^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9_])+$/gi;
        var flag = pattern.test(text);
        if (!flag)
        {
            throw '当前含有特殊字符';
        }
    }

    static checkUserNameValid(userName)
    {
        // var regex = "[a-zA-Z0-9_]{2,18}";
        // var reg = new RegExp(regex);
        // if (!reg.test(userName))
        // {
        //     throw '用户名长度必须大于两个字符';
        // }
        if (userName.length < 2)
        {
            throw '用户名长度必须大于两个字符';
        }
    }

    static checkPwdValid(pwd)
    {
        // var regex = "[a-zA-Z0-9_]{6,18}";
        // var reg = new RegExp(regex);
        // if (!reg.test(pwd))
        // {
        //     throw '密码必须大于两个字符';
        // }
        if (pwd.length < 6)
        {
            throw '密码长度不小于6位字符';
        }
    }

    static checkUserNameEqualsPwd(userName, pwd)
    {
        if (userName == pwd)
        {
            throw '用户名和密码不能相同';
        }
    }

    static checkEmpty(content)
    {
        if (content == '' || content == null)
        {
            throw '该字段不能为空';
        }
    }

    static checkEmpty(content, error)
    {
        if (content == '' || content == null)
        {
            throw error;
        }
    }

    static checkSessionInVaild(opcode)
    {
        if (opcode == '1001')
        {
            StorageUtils.save('session', '');
        }
    }

    static checkPwdEquals(newPwd, newRepeatPwd)
    {
        if (newPwd != newRepeatPwd)
        {
            throw '两次输入新密码不同';
        }
    }

    static checkTwoPwdEquals(oldPwd, newRepeatPwd)
    {
        if (oldPwd == newRepeatPwd)
        {
            throw '新密码和旧密码输入相同';
        }
    }

    static checkType(devicetype)
    {
        if (devicetype == '未知')
        {
            throw '设备编号不正确';
        }
    }

    static checkSelectGroup(grouptype)
    {
        if (grouptype == '选择群组类型')
        {
            throw '请选择群组类型';
        }
    }

    static checkWaterRunTime(title, editContext)
    {
        if (title == '输入运行时长(分钟)')
        {
            this.checkAllNumber(editContext)
            var waterRunTime = parseInt(editContext)
            if (!(waterRunTime > 0 && waterRunTime < 1081))
            {
                throw '运行时长允许范围1-1080分钟'
            }
        }
    }

    static checkCycleTimePerDay(title, editContext)
    {
        if (title == '输入单日循环(次数)')
        {
            this.checkAllNumber(editContext)
            var cycleTimePerDay = parseInt(editContext)
            if (!(cycleTimePerDay > 0 && cycleTimePerDay < 25))
            {
                throw '单日循环次数允许范围1-24次'
            }
        }
    }

    static checkCyclesPerDday(title, editContext)
    {
        if (title == '输入单日循环周期(分钟)')
        {
            this.checkAllNumber(editContext)
            var cyclesPerDday = parseInt(editContext)
            if (!(cyclesPerDday > 0 && cyclesPerDday < 1081))
            {
                throw '单日循环周期允许范围1-1080分钟'
            }
        }
    }

    static checkNonWaterDays(title, editContext)
    {
        if (title == '输入间隔天数(天)')
        {
            this.checkAllNumber(editContext)
            var nonWaterDays = parseInt(editContext)
            if (!(nonWaterDays > 0 && nonWaterDays < 32))
            {
                throw '间隔天数允许范围1-31天'
            }
        }
    }

    static checkGatewayName(title, editContext)
    {
        if (title == '修改设备名称')
        {
            if (editContext.length > 30)
            {
                throw '设备名称允许范围30位'
            }
        }
    }

    static checkGatewayDesc(title, editContext)
    {
        if (title == '修改备注信息')
        {
            if (editContext.length > 20)
            {
                throw '备注信息允许范围20位'
            }
        }
    }

    static checkCustomWeek(wateringStartMode, wateringCycleMode, customWeek)
    {
        if (wateringStartMode == '0')
        {
            if (wateringCycleMode == '0')
            {
                if (customWeek == '')
                {
                    throw '请选择循环日期'
                }
            }
        }
    }

    static checkWaterEveryDays(wateringStartMode, wateringCycleMode, waterEveryDays)
    {
        if (wateringStartMode == '0')
        {
            if (wateringCycleMode == '1')
            {
                if (waterEveryDays == '' || waterEveryDays == '0')
                {
                    throw '请选间隔天数'
                }
            }
        }
    }

    static checkInputLess10(number)
    {
        this.checkAllNumber(number)
        if (parseInt(number) < 10)
        {
            throw '比例最小设定值为10%'
        }
    }

    static checkGatewaySNLength(gatewaySN)
    {
        if (gatewaySN.length < 10)
        {
            throw '设备编号长度不正确'
        }
    }

    static checkUnSelect(tip, errror)
    {
        if (tip == '请选择')
        {
            throw errror;
        }
    }

    static checkUnSelectResult(tip, equals)
    {
        if (tip == equals)
        {
            throw equals;
        }
    }

    static converState(state)
    {
        if (state == '0')
        {
            return '草稿'
        }
        if (state == '1')
        {
            return '待核实派发'
        }
        if (state == '2')
        {
            return '待核实'
        }
        if (state == '3')
        {
            return '待立案'
        }
        if (state == '4')
        {
            return '待督办'
        }
        if (state == '5')
        {
            return '待处理派发'
        }
        if (state == '6')
        {
            return '待处理指派'
        }
        if (state == '7')
        {
            return '待处理'
        }
        if (state == '8')
        {
            return '待核查派发'
        }
        if (state == '9')
        {
            return '待核查'
        }
        if (state == '10')
        {
            return '待核查审核'
        }
        if (state == '11')
        {
            return '待结案'
        }
        if (state == '12')
        {
            return '待归档'
        }
        if (state == '13')
        {
            return '已归档'
        }
        if (state == '14')
        {
            return '返工'
        }
        if (state == '15')
        {
            return '复查'
        }
        if (state == '16')
        {
            return '已退回'
        }
        if (state == '17')
        {
            return '待延期审核'
        }
        if (state == '18')
        {
            return '待缓办审核'
        }
        if (state == '19')
        {
            return '待回退审核'
        }
    }

    static checkUploadPics(uploadPics, tip)
    {
        if (uploadPics.length == 0)
        {
            throw '请至少上传一张照片'
        }

    }

    static checkLocation(geoAddress)
    {
        if (geoAddress == '请选择打卡位置')
        {
            throw '请选择打卡位置'
        }
    }

    static getTimeLineTime(timeLineList, status)
    {
        var obj = {};
        if (timeLineList != null)
        {
            for (let i = 0; i < timeLineList.length; i++)
            {
                let item = timeLineList[i];
                if (item.businessKey == status)
                {

                    obj = {'limitSeconds': item.limitSeconds, 'limitTime': item.limitTime.replace('T', ' '), 'startTime': item.startTime.replace('T', ' '), 'endTime': item.endTime.replace('T', ' '), 'user': item.taskUserName, 'result': item.ret == '2' ? '通过' : '不通过', 'geo': item.geo, 'geoDescript': item.geoDescript, 'comment': item.comment, 'pics': item.pics, 'audios': item.audios};
                }
            }
            return obj;
        } else
        {
            obj = {'starTime': '暂无', 'endTime': '暂无', 'user': '暂无', 'result': '暂无', 'geo': '暂无', 'geoDescript': '暂无', 'comment': '暂无', 'pics': '暂无', 'audios': '暂无'};
            return obj;
        }
    }
}
